package com.keepers.repair.adapter

import android.support.v7.widget.CardView
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.keepers.repair.R
import com.keepers.repair.model.UserModel
import org.jetbrains.anko.find

class UserAdapter(private val list: List<UserModel>) : RecyclerView.Adapter<UserAdapter.ViewHolder>() {

    override fun getItemCount() = list.size
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
        val view = LayoutInflater.from(p0.context).inflate(R.layout.item_user, p0, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = list[position]
        holder.name.text = item.Name
        if (item.Online == true) {
            holder.card.alpha = 1F
            holder.state.text = "[在线]"
        } else {
            holder.card.alpha = 0.5F
            holder.state.text = "[离线]"
        }
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val card = view.find<CardView>(R.id.item_user_card)
        val name = view.find<TextView>(R.id.item_user_name)
        val state = view.find<TextView>(R.id.item_user_state)
    }
}
